<template>
	<view class="content">
		<view class="app-title ac">
			<view class="fl" @click="back"><image class="back" src="/static/img/back.png"></image></view>更换设备
		</view>
		
		<view class="top-bg"></view>
		
		<view class="detail">
			<u-cell-group>
				<u-cell-item title="下道去向" :value="data_list.xdgoto == '' ? '请选择' : data_list.xdgoto" @click="show_picker=true;columns=go_options"></u-cell-item>
				<u-cell-item title="下道机构" :value="down == '' ? '请选择' : down" @click="show_picker=true;columns=org_options;is_org=true;"></u-cell-item>
				<u-cell-item title="上道机构" :value="on == '' ? '请选择' : on"  @click="show_picker=true;columns=org_options;is_org=false;"></u-cell-item>
				<u-cell-item title="更换人" :value="username" :arrow="false"></u-cell-item>
				<view style="margin:10px 0"><b>器材信息/下道信息</b></view>
				<u-cell-item title="下道扫描" :arrow="false"><view class="fr" @click="codeCheck('down')"><image class="code" src="/static/img/barcode.png"></image></view></u-cell-item>
				<u-cell-item title="器材类型" :value="down_device.eqtTypeName" :arrow="false"></u-cell-item>
				<u-cell-item title="器材型号" :value="down_device.eqtModelNum" :arrow="false"></u-cell-item>
				<u-cell-item title="所在机构" :value="down_device.orgName" :arrow="false"></u-cell-item>
				<u-cell-item title="所在站" :value="down_device.stationName" :arrow="false"></u-cell-item>
				<u-cell-item title="条形码" :value="down_device.zcCode" :arrow="false"></u-cell-item>
				<u-cell-item title="生产厂家" :value="down_device.manufacturer" :arrow="false"></u-cell-item>
				<u-cell-item title="备注" :value="down_device.note" :arrow="false"></u-cell-item>
				<view style="margin:10px 0"><b>器材信息/上道信息</b></view>
				<u-cell-item title="上道扫描" :arrow="false"><view class="fr" @click="codeCheck"><image class="code" src="/static/img/barcode.png"></image></view></u-cell-item>
				<u-cell-item title="器材类型" :value="on_device.eqtTypeName" :arrow="false"></u-cell-item>
				<u-cell-item title="器材型号" :value="on_device.eqtModelNum" :arrow="false"></u-cell-item>
				<u-cell-item title="所在机构" :value="on_device.orgName" :arrow="false"></u-cell-item>
				<u-cell-item title="所在站" :value="on_device.stationName" :arrow="false"></u-cell-item>
				<u-cell-item title="条形码" :value="on_device.zcCode" :arrow="false"></u-cell-item>
				<u-cell-item title="生产厂家" :value="on_device.manufacturer" :arrow="false"></u-cell-item>
				<u-cell-item title="备注" :value="on_device.note" :arrow="false"></u-cell-item>
			</u-cell-group>
		</view>
			
		<view class="work-bottom"><button class="default-btn" @click="pushData">完成</button></view>
		
		<view class="full" v-show="show_picker"></view>
		<u-select v-model="show_picker" :list="columns" label-name="text" @confirm="onConfirm" @cancel="onCancel"></u-select>
		<!-- <Picker v-show="show_picker" show-toolbar :columns="columns" @confirm="onConfirm" @cancel="onCancel"></Picker> -->
	</view>
</template>

<script>
export default{
	data() {
		return {
			username : uni.getStorageSync('username') || '',
			org_options:'',
			go_options:'',
			columns : [],
			on_device : '',
			down_device : '',
			data_list:{
				// eqtList:[{
				//         "equId": "string",  //设备id
				//         "equModel": "string", //设备型号
				//         "equName": "string", //设备名  (上道位置，下道位置)
				//         "equTypeId": "string", //设备类型Id
				//         "equTypeName": "string", //设备类型名
				//         "factory": "string", //生产厂家
				//         "note": "string", //上下道设备位置备注
				//         "orgId": "string", //机构id
				//         "orgName": "string", //机构名称
				//         "propName": "string", //属性名称
				//         "propValue": "string", //属性值
				//         "sdTime": "", //上道时间
				//         "stationDbm": "string", //车站码
				//         "stationName": "string", //车站名
				//         "zcCode": "string", //器材条码
				//         "zcId": "string", //zc表中id
				//         "zcName": "string" //器材名
				//       }
				//     ],
				    "notes": [   //上道位置备注
				      "string"
				    ],
				    "option": "saveEqtReplace",  //操作   saveEqtSd 上道，  saveEqtXd 下道saveEqtReplace 更换
				    "sdDbm": "string", //上道车站
				    "sdEqtZCid": [  //上道器材id
				      "1"
				    ],
				    "sdEqtid": [  //上道设备id
				      "2"
				    ],
				    "uid": this.getUserId(), //用户id
				    "workId": "1", //工作id
				    "xdDbm": "string", //下道车站
				    "xdEqtZCids": [],//下道器材id
				    "xdEqtids": [],//下道设备id
				    "xdgoto": ""  //下道去向0下道送厂家，1仓库
			},
			on : '',
			down : '',
			is_org : true,
			show_picker:false
		}
	},
	created(){
		this.initOptions();
	},
	methods:{
		back() {
			uni.navigateBack();
		},
		onConfirm(value, index) {
			if(value[0].extra == 'org') {
				this.is_org ? this.down = value[0].label : this.on = value[0].label;
			}else if(value[0].extra == 'go'){
				this.data_list.xdgoto = value[0].label;
			}
			this.onCancel();
		},
		onCancel() {
			this.show_picker = false;
		},
		initOptions(){
			this.fetch('GET',this.baseUrl()+'/home/getOrgTreeByPerson',{personId:this.getUserId()}).then((res)=>{
				if(res.returnCode == 1) {
					this.org_options = res.data;
					this.org_options.map((item)=>{
						item.children = '';
						item.extra = 'org';
					})
				}
			})
			this.fetch('GET',this.baseUrl()+'/eqtTZcController/queryEqtDic',{dicClass:'CHUKU_QUXIANG'}).then((res)=>{
				if(res.returnCode == 1) {
					this.go_options = res.data;
					this.go_options.map((item)=>{
						item.children = '';
						item.extra = 'go';
						item.text = item.dicname;
					})
				}
			})
		},
		codeCheck(type) {
			 // 允许从相机和相册扫码
			uni.scanCode({
			    success: (res) => {
			        if(res) {
						this.fetch('GET',this.baseUrl()+'/eqtTZcController/scanCode',{action:'3',code:res.result}).then((res)=>{
							if(res.returnCode == 1) {
								if(type == 'down') {
									this.data_list.sdEqtid = [res.data.id];
									this.data_list.sdEqtZCid = [res.data.inUseEqtVo.sdId];
									this.data_list.notes = [res.data.note];
									this.data_list.sdDbm = res.data.stationName;
									this.down_device = res.data;
								}else{
									this.data_list.xdEqtids = [res.data.id];
									this.data_list.xdEqtZCids = [res.data.inUseEqtVo.sdId];
									this.data_list.xdDbm = res.data.stationName;
									this.on_device = res.data;
								}
							}
						})
					}
				}
			});
		},
		pushData() {
			if(this.data_list.xdgoto == '') return uni.showToast({title:'请选择下道去向',icon:'none'});
			if(this.data_list.sdEqtid.length == 0 || this.data_list.xdEqtids.length == 0) return uni.showToast({title:'请选择器材',icon:'none'});
			this.fetch('POST',this.baseUrl()+'/eqtTZcController/saveSdXd',this.data_list,true).then((res)=>{
				if(res.returnCode == 1) {
					uni.showToast({title:res.message,icon:'none'});
				}else{
					uni.showToast({title:res.message,icon:'none'});
				}
			})
		}
	}
}
</script>

<style scoped>
	.detail{
		height: auto;padding-bottom: 60px;
	}
	.default-btn{
		width: 90%;margin: 0 auto;margin-top: 20px;
	}
	.work-bottom{
		position: fixed;left: 0;bottom: 2%;width:100%;z-index: 5;
	}
	.code{
		width: 15px;height: 15px;
	}
	.van-cell__value{
		color: #3E3A39;
	}
</style>
